﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Cleware.Debit3.Tools.EdmUtil
{
    public class Config
    {
        public static Config Instance = new Config();

        /// <summary>
        /// edmx file
        /// </summary>
        public readonly string Edmx;
        /// <summary>
        /// ssdl file
        /// </summary>
        public readonly string Ssdl;
        /// <summary>
        /// csdl file
        /// </summary>
        public readonly string Csdl;
        /// <summary>
        /// msl file
        /// </summary>
        public readonly string Msl;
        /// <summary>
        /// connection string 
        /// </summary>
        public readonly string ConnectionString;
        /// <summary>
        /// feldolgozandó tárolt eljárások
        /// ha üres, akkor az összeset feldolgozza
        /// </summary>
        public readonly List<string> SPList;
        /// <summary>
        /// Azok a tárolt eljárások, amiket kihagy a feldolgozásból
        /// </summary>
        public readonly List<string> Exclusions;

        public readonly bool IgnoreStoredProcedures;

        public readonly string XmlDescription;

        private Config()
        {
            try
            {
                Edmx = GetConfigValue("edmx");
                Ssdl = GetConfigValue("ssdl");
                Csdl = GetConfigValue("csdl");
                Msl = GetConfigValue("msl");
                ConnectionString = GetConfigValue("connectionstring");
                IgnoreStoredProcedures = this.GetBoolConfigValue("ignoreprocgen");

                string stringValue;

                SPList = null;
                stringValue = GetConfigValue("splist");
                if (!string.IsNullOrEmpty(stringValue))
                {
                    stringValue = stringValue.Replace("\r\n", "");
                    SPList = new List<string>(stringValue.Split(','));
                }

                Exclusions = null;
                stringValue = GetConfigValue("exclusions");
                if (!string.IsNullOrEmpty(stringValue))
                {
                    stringValue = stringValue.Replace("\r\n", "");
                    Exclusions = new List<string>(stringValue.Split(','));
                }
                XmlDescription = this.GetConfigValue("xmldescription");
            }
            catch (Exception ex)
            {
                Logger.Instance.Log(string.Format("ERROR: Invalid config file! Exception: {0}", ex.ToString()));
            }
        }

        private string GetConfigValue(string key)
        {
            string retval = null;

            if (ConfigurationManager.AppSettings.AllKeys.Contains<string>(key))
                retval = ConfigurationManager.AppSettings[key];

            return retval;
        }

        private bool GetBoolConfigValue(string key)
        {
            bool boolRet = false;

            if (ConfigurationManager.AppSettings.AllKeys.Contains<string>(key))
            {
                string retval = ConfigurationManager.AppSettings[key];
                bool.TryParse(retval, out boolRet);
            }

            return boolRet;
        }
    }
}
